package com.example.shop_2305.fg

import android.app.Activity
import android.content.Intent
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.bumptech.glide.Glide
import com.example.lib_base.mvvm.BaseViewFragment
import com.example.shop_2305.R
import com.example.shop_2305.databinding.FragmentMineBinding
import com.example.shop_2305.utils.GlideUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector


class MineFragment : BaseViewFragment<FragmentMineBinding>() {
    override fun initView() {
   /*     val hostFragment =
            childFragmentManager.findFragmentById(R.id.fragmentContainerView2) as NavHostFragment
        val navController = hostFragment.navController
        binding.bottomMine.setupWithNavController(navController)*/
        binding.bottomMine.setupWithNavController(requireActivity().findNavController(R.id.fragmentContainerView2))

        //选项弹出框
        binding.userSex.setOnClickListener {
            val list = mutableListOf("男","女","保密","不详")
            val optionPicker = OptionPicker(requireActivity())
            optionPicker.setTitle("请选择性别")
            optionPicker.setData(list)
            optionPicker.setOnOptionPickedListener { position, item ->
                binding.userSex.text = list[position]
            }
            optionPicker.show()
        }
        //日期弹出框
        binding.userBirth.setOnClickListener {
            val datePicker = DatePicker(requireActivity())
            datePicker.setTitle("请选择出生年月日")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today(),
                DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.userBirth.text = "$year-$month-$day"
            }
            datePicker.show()
        }
        //地址弹出框
        binding.userAddress.setOnClickListener {
            val addressPicker = AddressPicker(requireActivity())
            addressPicker.setTitle("请选择配送地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.userAddress.text = "${province.name}${city.name}${county.name}"
            }
            addressPicker.show()
        }
        binding.userIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
    }

    override fun initData() {

    }

    override fun getLayout(): Int  = R.layout.fragment_mine
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE&&resultCode==Activity.RESULT_OK){
            val bean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            context?.let { GlideUtils.loadCircle(it,bean.path,binding.userIv) }
        }
    }

}